<?php

namespace Biz\model\advertisement;

use Medoo\Medoo;

class MPositionBaseInfo extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_position';

    const _CONFIG_SELECT_ = 'db_position';

    const _DATABASE_ = 'hhz_position';

    const _TABLE_NAME = 'position_base_info';


    const STATUS_NORMAL = 1;
    // 状态为删除
    const STATUS_DELETE = 9;
    const STATUS_RELEASE = 2;//占位释放

    const IS_NOT_SHARE = 1;
    const IS_SHARE = 2;
    const ADM_IS_SHARE_MAP = [
        self::IS_SHARE => '可共享',
        self::IS_NOT_SHARE => '不共享',
    ];
    const SORT_TYPE_TOP = 1;
    const SORT_TYPE_BOTTOM = 2;
    const ADM_SORT_TYPE_MAP = [
        self::SORT_TYPE_TOP => '置顶',
        self::SORT_TYPE_BOTTOM => '置底'
    ];

    //投放原因
    const REASON_TYPE_BUSINESS = 1;
    const REASON_TYPE_KA = 2;
    const REASON_TYPE_OPERATE = 3;
    const REASON_TYPE_NEW_USER = 4;
    const REASON_TYPE_SUPER = 5;
    const REASON_TYPE_PRODUCT = 6;
    const REASON_MAP = [
        self::REASON_TYPE_SUPER => '特大型活动',
        self::REASON_TYPE_BUSINESS => '商业广告',
        self::REASON_TYPE_KA => '设计师广告',
        self::REASON_TYPE_OPERATE => '运营推广/市场合作',
        self::REASON_TYPE_NEW_USER => '新用户承接',
        self::REASON_TYPE_PRODUCT => '产品形态',
    ];
    const REASON_SIGN_MAP = [
        self::REASON_TYPE_SUPER => 'large_events',
        self::REASON_TYPE_BUSINESS => 'biz_ad',
        self::REASON_TYPE_KA => 'disigner_ad',
        self::REASON_TYPE_OPERATE => 'op_suggest',
        self::REASON_TYPE_NEW_USER => 'welcome_new_user',
        self::REASON_TYPE_PRODUCT => 'product_form',
    ];
    //投放目的
    const AIM_MAP = [
        '内容生产向',
        '内容消费向',
        '服务咨询向',
        'WIKI向',
        '商业广告投放',
        '其他',
    ];
    //关闭时效
    const CLOSE_TYPE_NONE = 9; //无
    const CLOSE_TYPE_ONCE_VIEW = 1; //单次浏览
    const CLOSE_TYPE_ONCE_INIT = 2; //单次启动App
    const CLOSE_TYPE_DAY = 3; //当日不再展示
    const CLOSE_TYPE_MAP = [
        self::CLOSE_TYPE_ONCE_VIEW => '单次浏览',
        self::CLOSE_TYPE_ONCE_INIT => '单次启动App',
    ];

    //展示时间
    const SHOW_TYPE_TIME = 1;
    const SHOW_TYPE_ALWAYS = 2;
    const SHOW_TYPE_MAP = [
        self::SHOW_TYPE_TIME => '指定时间段',
        self::SHOW_TYPE_ALWAYS => '永久生效',
    ];

    public static function getRow($id)
    {
        return self::select('*', ['id' => $id]);
    }

    public static function apiActiveList($type, $nowDate)
    {
        $where = [
            'OR' => [
                'end_show_time[>]' => $nowDate,
                'show_type' => self::SHOW_TYPE_ALWAYS
            ],
            'position_id' => $type,
            'info_status' => self::STATUS_NORMAL,
            'ORDER' => [
                'pos',
                'sort'
            ]
        ];
        return self::select_master('*', $where);
    }

    public static function apiPreviewList($relIds)
    {
        $where = [
            'rel_id' => $relIds,
            'info_status' => self::STATUS_NORMAL,
            'ORDER' => [
                'pos',
                'sort'
            ]
        ];
        return self::select_master('*', $where);
    }

    public static function top($relId, $sort)
    {
        if (!$relId) {
            return;
        }
        $where = [
            'rel_id' => $relId,
        ];

        self::update(['sort' => $sort - 1], $where);
    }

    public static function getActiveCount($position_ids, $nowDate)
    {
        return self::select([
            'position_id',
            'count' => Medoo::raw("COUNT(`id`)")
        ], [
            'AND' => [
                'OR #first' => [
                    'end_show_time[>]' => $nowDate,
                    'show_type' => self::SHOW_TYPE_ALWAYS
                ],
                'position_id' => $position_ids,
                'info_status' => self::STATUS_NORMAL,
            ],
            'GROUP' => [
                'position_id'
            ]
        ]);
    }

    public static function getTopSortByPositionId($positionId, $pos = 0)
    {
        if (!$positionId) {
            return;
        }

        $where = [
            'position_id' => $positionId,
            'info_status' => self::STATUS_NORMAL,
            'pos' => $pos
        ];

        return self::select(['max' => Medoo::raw("min(`sort`)")], $where);
    }

    public static function getCountByRelIds($relIds)
    {
        if (!$relIds) {
            return [];
        }

        $where = [
            'rel_id' => $relIds,
            'info_status' => self::STATUS_NORMAL,
            'GROUP' => ['rel_id'],
        ];
        $res = self::select(['rel_id', 'nums' => Medoo::raw("count(1)")], $where);

        if (!$res) {
            return [];
        } 
        
        return array_column($res, 'nums', 'rel_id');
    }
}

